﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using System.Web;
using System.IO;
using System.Drawing;


namespace Eshouse.Bll
{
   public class publicComm
    {
       public static string GetJsonString(DataTable dt, int rowCount)
       {
           StringBuilder sbResult = new StringBuilder();
           int colCount = dt.Columns.Count;
           sbResult.Append("{\"total\":" + rowCount.ToString() + ",\"rows\":[");
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               DataRow tmpRow = dt.Rows[i];
               sbResult.Append("{");
               for (int j = 0; j < colCount; j++)
               {
                   sbResult.Append(string.Format("\"{0}\":\"{1}\"", dt.Columns[j].ColumnName, tmpRow[j]));
                   if (j < colCount - 1)
                       sbResult.Append(",");
               }
               sbResult.Append("}");
               if (i < dt.Rows.Count - 1)
                   sbResult.Append(",");
           }
           sbResult.Append("]}");
           return sbResult.ToString();
       
       }
       /// <summary>
       /// 静态加密方法
       /// </summary>
       /// <param name="s">需要加密的字符串</param>
       /// <returns>返回一个加密后的字符串</returns>
       public static String toMD5(String s) //把字符串“s”用MD5算法编码
       {
           MD5 md5 = new MD5CryptoServiceProvider();

           byte[] aa = System.Text.Encoding.UTF8.GetBytes(s);
           byte[] res = md5.ComputeHash(aa);
           StringBuilder bb = new StringBuilder();
           for (int i = 0; i < res.Length; i++)
           {
               if (res[i] < 16)
               {
                   bb.Append("0");
               }
               bb.Append(res[i].ToString("x"));
           }
           return bb.ToString();
       }
       /// <summary>
       /// 产生验证码
       /// </summary>
       public static string GetCode
       {
           get
           {

               int number;
               char code;
               string checkCode = String.Empty;
               System.Random random = new Random();
               for (int i = 0; i < 5; i++)
               {
                   number = random.Next();
                   if (number % 2 == 0)
                       code = (char)('0' + (char)(number % 10));
                   else
                       code = (char)('A' + (char)(number % 26));
                   checkCode += code.ToString();
               }
               return checkCode;
           }
       }

       /// <summary>
       /// 获取随机图片码
       /// </summary>
       /// <returns>文件名</returns>
       public static string GetImge(string checkCode)
       {

           if (checkCode == null || checkCode.Trim() == String.Empty)
               return "";
           CRC32Primarykey crc = new CRC32Primarykey();

           string fileName =crc.createIntID(DateTime.Now.ToString("yyyyMMddhhmmss")+checkCode) + @".gif";
           ///创建一个用于临时保存随机图片的文件夹，如果文件夹存在先清空里面的文件
           string imgPath = HttpContext.Current.Server.MapPath("~/UploadFiles/chkImgTmp/");
           DirectoryInfo dr = new DirectoryInfo(imgPath);
           if (!dr.Exists)
           {
               try
               {
                   dr.Create();
               }
               catch (Exception ex)
               {
                   logRecord.RecordErr("创建获取验证码文件夹", ex);
               }
           }
           else
           {
               foreach (FileInfo fi in dr.GetFiles())
               {
                   fi.Delete();
               }
           }
           System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
           Graphics g = Graphics.FromImage(image);
           try
           {
               System.Random random = new Random();
               //清空图片背景色
               g.Clear(Color.White);
               //画图片的背景噪音线
               for (int i = 0; i < 25; i++)
               {
                   int x1 = random.Next(image.Width);
                   int x2 = random.Next(image.Width);
                   int y1 = random.Next(image.Height);
                   int y2 = random.Next(image.Height);
                   g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
               }
               Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
               System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
               g.DrawString(checkCode, font, brush, 2, 2);
               //画图片的前景噪音点
               for (int i = 0; i < 100; i++)
               {
                   int x = random.Next(image.Width);
                   int y = random.Next(image.Height);
                   image.SetPixel(x, y, Color.FromArgb(random.Next()));
               }
               //画图片的边框线
               g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

               image.Save(imgPath + fileName, System.Drawing.Imaging.ImageFormat.Gif);
           }
           catch (Exception e)
           {
               logRecord.RecordErr("创建验证图片", e);
               return "";
           }
           finally
           {
               g.Dispose();
               image.Dispose();
           }
           return @"/UploadFiles/chkImgTmp/" + fileName;
       }


    }
}
